﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using FrbaBus.Core;

namespace FrbaBus.Cancelar_Viaje
{
    public partial class CancelarEncomienda : Form
    {
        public CancelarEncomienda()
        {
            InitializeComponent();
        }

        private void CancelarEncomienda_Load(object sender, EventArgs e)
        {
            dateTimePicker1.MinDate = Convert.ToDateTime(System.Configuration.ConfigurationManager.AppSettings["Fecha"]);
        }
        public string query = null;
        public DateTime dia;
        public string diaHora;
        public bool first;
        public DataGridViewRow Row;
        public DataGridViewCell Cell;
        public DataGridViewColumn Column;
        private void addpathern()
        {
            if (first == true)
            {
                first = false;
                query = query + " where";
            }
            else
                query = query + " AND";
        }

        public string columnas = "ALL_IN.Disponibilidad_kg.Nro_Transaccion,ALL_IN.Disponibilidad_kg.Nro_Paquete,ALL_IN.Disponibilidad_kg.Cli_DNI,ALL_IN.Disponibilidad_kg.Fecha_Salida,ALL_IN.Disponibilidad_kg.Mic_Patente,ALL_IN.Disponibilidad_kg.Monto,ALL_IN.Disponibilidad_kg.ID_Recorrido";
            

        private void button1_Click(object sender, EventArgs e)
        {


            query = " FROM ALL_IN.Disponibilidad_kg WHERE NOT EXISTS (SELECT * FROM ALL_IN.Cancelacion WHERE ALL_IN.Disponibilidad_kg.Nro_Paquete = ALL_IN.Cancelacion.Nro_Paquete) and (ALL_IN.Disponibilidad_kg.Fecha_Salida >= (select * from ALL_IN.fecha_Actual))";
            dataGridView1.Rows.Clear();
            DBManager db = new DBManager();
            first = true;
            if (textBox1.Text != "")
            {
                query = query + " and ALL_IN.Disponibilidad_kg.Cli_DNI like '%" + textBox1.Text + "%'";
            }

            if (textBox3.Text != "")
            {
                query = query + " and ALL_IN.Disponibilidad_kg.Nro_Paquete like '%" + textBox3.Text + "%'";
            }

            if (textBox4.Text != "")
            {
                query = query + " and ALL_IN.Disponibilidad_kg.Nro_Transaccion like '%" + textBox4.Text + "%'";
            }
            if (checkBox1.Checked == true)
            {
                dia = dateTimePicker1.Value;
                diaHora = dia.ToString("yyyy-MM-dd");
                diaHora = diaHora + " 00:00:00.000";
                query = query + " and ALL_IN.Disponibilidad_kg.Fecha_Salida between '" + diaHora + "' and (DATEADD(day, 1,'" + diaHora + "'))";
            }

            SqlCommand consulta = new SqlCommand("select " + columnas + query);
            SQLResponse r = db.executeQuery(consulta);
            SqlCommand consulta2 = new SqlCommand("select COUNT (*) " + query);
            SQLResponse r2 = db.executeQuery(consulta2);
            int valor = (int)r2.result.Rows[0][0];
            if (valor == 0)
            {
                MessageBox.Show("No hay resultados");
            }
            else
            {
                for (int i = 0; i < valor; i++)
                {
                    dataGridView1.Rows.Add(r.result.Rows[i][0].ToString(), r.result.Rows[i][1].ToString(), r.result.Rows[i][2].ToString(), r.result.Rows[i][3].ToString(), r.result.Rows[i][4].ToString(), r.result.Rows[i][5].ToString(), r.result.Rows[i][6].ToString());
                }
            }

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                dateTimePicker1.Enabled = true;

            }
            else
            {
                dateTimePicker1.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            DialogResult dialogResult = MessageBox.Show("¿Quiere cancelar las encomiendas seleccionadas?", "Cancelar Encomiendas", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
                Int32 selectedIndexCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
                if (selectedRowCount == 0)
                {
                    MessageBox.Show("Debe seleccionar una encomienda");
                }
                else
                {
                    DBManager db = new DBManager();
                    for (int i = 0; i < selectedRowCount; i++)
                    {
                        Row = dataGridView1.SelectedRows[i];
                        string NroTransac = Row.Cells[0].Value.ToString();
                        string cliDNI = Row.Cells[2].Value.ToString();
                        string nroPaquete = Row.Cells[1].Value.ToString();
                        string motivo = textBox2.Text;
                        string fecha = Row.Cells[3].Value.ToString();
                        string monto = Row.Cells[5].Value.ToString();
                        SqlCommand consultaX = new SqlCommand("exec ALL_IN.cancelaPaquete @NroTransaccion = '" + NroTransac
                            + "', @cliDni= '" + cliDNI + "', @nroPaquete= '" + nroPaquete + "', @motivo= '" + motivo + "'");
                        db.executeQuery(consultaX);
                    }
                    MessageBox.Show("Se ha realizado la cancelacion de las encomiendas.");
                    this.Close();
                }

            }
        }
    }
}
